var request = require('superagent');
var API = require('./support/const');
var service = require('../../service/log')

exports.report = {
    list: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.get(API.reports.list)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getList', 'msg': '获取报告列表'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
    notice_list: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        let url = API.reports.notice_list
        if (params.condition.school) {
            url = API.tasks.task_notice_list
        }
        request.get(url)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getList', 'msg': '获取报告列表'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
    detail:function (req,res,next) {
        let header = {
            token: req.cookies.token
        };
        let {id} = req.body;
        request.get(API.reports.detail + id)
            .set(header)
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getId', 'msg': '获取任务详情'}, req);
                }
                res.json(JSON.parse(_res.text));
            });

    },
    update: function (req, res, next) {

        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.put(API.reports.update)
            .type('application/json')
            .set(header)
            .send(JSON.stringify(params))
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '管理员编辑检查报告', 'mark':params }, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
    create_notice_file:function (req,res,next) {

            let header ={
                token: req.cookies.token
            };
            let {id, type} = req.body;
            request.get(API.reports.create_notice_file + id + '?type=' + type)
                .set(header)
                .end(function(_req,_res){
                    if(_res.status ==200){
                        service.save_user_log({'type': 'getId', 'msg': '生成通知书' }, req);
                    }
                    res.json(JSON.parse(_res.text));
                })

    },
    pass_notice:function (req,res,next) {
            let header ={
                token: req.cookies.token
            };
            let id = req.body.id;
            let msg = JSON.stringify(req.body.msg);
            request.get(API.reports.pass_notice + id + '?msg=' + msg)
                .set(header)
                .end(function(_req,_res){
                    if(_res.status ==200){
                        service.save_user_log({'type': 'getId', 'msg': '生成通知书'}, req);
                    }
                    res.json(JSON.parse(_res.text));
                })

    },
    issue_notice:function (req,res,next) {

            let header ={
                token: req.cookies.token
            };
            let id = req.body.id;
            let msg = JSON.stringify(req.body.msg);
            // request.get(API.reports.issue_notice + id + '?msg=' + msg)
            request.get(API.reports.issue_notice + id )
                .set(header)
                .query({msg:msg})
                .end(function(_req,_res){
                    console.log(_res)
                    if(_res.status ==200){
                        service.save_user_log({'type': 'getId', 'msg': '下发通知书'}, req);
                    }
                    res.json(JSON.parse(_res.text));
                })

    },
    returnReport:function(req,res,next){
        let header ={
            token: req.cookies.token
        };
        let params = req.body;
        request.put(API.reports.return_report)
            .set(header)
            .query({msg:JSON.stringify(params)})
            .end(function(_req,_res){
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getId', 'msg': '打回报告'}, req);
                }
                res.json(JSON.parse(_res.text));
            })
    }
    ,
    //上传扫描件
    save_scanId:function(req,res,next){
        let header ={
            token: req.cookies.token
        };
        let params = req.body;
        request.put(API.reports.save_scanId)
            .set(header)
            .query({msg:JSON.stringify(params)})
            .end(function(_req,_res){
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getId', 'msg': '保存扫描件'}, req);
                }
                res.json(JSON.parse(_res.text));
            })
    }
}
